<template>
  <div class="main-layout">
    <el-card class="box-card" shadow="never">
      <el-page-header title="返回" :icon="ArrowLeft" @back="goBack">
        <template #content>
          <span class="text-large font-600 mr-3">文章详情</span>
        </template>
      </el-page-header>

      <div class="news-detail" v-loading="loading">
        <div class="title">
          <h1>{{ articleData.title }}</h1>
        </div>
        
        <div class="subtitle">
          <p>作者：{{ articleData.publisher_id }}  发布日期：{{ formatDate(articleData.publish_time || '') }}</p>
        </div>

        <div class="content">
          <div class="text">
            <div v-html="articleData.content"></div>
          </div>

          <!-- 如果有文章图片 -->
          <div class="article-image" v-if="articleData.pic_url">
            <img :src="articleData.pic_url" alt="文章配图">
          </div>

          <div class="article-info">
            <p>阅读量：{{ articleData.hits }}</p>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import ArticleApi from '@/api/article'
import type { ArticleData } from '@/api/article/types'
import { ElMessage } from 'element-plus'
import { ArrowLeft } from '@element-plus/icons-vue'

const router = useRouter()
const loading = ref(true)
const articleData = ref<ArticleData>({
  article_id: 0,
  publisher_id: 0,
  title: '',
  content: '',
  hits: 0,
  pic_url: '',
  publish_time: '',
  review_status: 0,
  is_recommend: 0,
  is_delete: 0,
})

// 格式化日期
const formatDate = (dateStr: string) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}`
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}

// 获取文章详情
const getArticleDetail = async (articleId: string) => {
  try {
    loading.value = true
    const response = await ArticleApi.getArticle(articleId)
    if (response.data) {
      articleData.value = response.data
    }
  } catch (error) {
    ElMessage.error('获取文章详情失败')
    console.error('获取文章详情失败:', error)
  } finally {
    loading.value = false
  }
}


onMounted(() => {
  // 从路由参数获取文章ID
  const articleId = router.currentRoute.value.params.id as string
  if (articleId) {
    getArticleDetail(articleId)
  } else {
    ElMessage.error('文章ID不存在')
    router.push('/article') // 或其他适当的错误处理
  }
})
</script>

<style scoped>
.news-detail {
  text-align: left;
  margin: 20px;
  max-width: 800px;
  margin: 20px auto;
}

.title {
  margin: 30px 0 20px;
  text-align: center;
}

.title h1 {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.subtitle {
  text-align: center;
  color: #666;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
}

.content {
  margin-top: 30px;
}

.text {
  font-size: 16px;
  line-height: 1.8;
  color: #333;
}

.article-image {
  margin: 20px 0;
  text-align: center;
}

.article-image img {
  max-width: 100%;
  height: auto;
  border-radius: 4px;
}

.article-info {
  margin-top: 30px;
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 4px;
  color: #666;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .news-detail {
    margin: 10px;
  }

  .title h1 {
    font-size: 20px;
  }

  .text {
    font-size: 15px;
  }
}
</style>
